package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

//新增图书操作
public class AddOperation implements IOperation {

    @Override
    public void work(BookList bookList) {
        System.out.println("新增图书！");

        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入新增图书名字：");
        String name = scanner.nextLine();

        System.out.println("请输入新增图书作者：");
        String author = scanner.nextLine();

        System.out.println("请输入新增图书价格：");
        int price = scanner.nextInt();
        //注意回车读入
        scanner.nextLine();

        System.out.println("请输入新增图书类型：");
        String type = scanner.nextLine();


        Book book = new Book(name,author,price,type);

        //默认放到当前，最后一本书的后面
        //检查数组当中，有没有这本书
        int currentSize = bookList.getUsedSize();
        for (int i = 0; i < currentSize; i++) {
            Book book1 = bookList.getBooks(i);
            if (name.equals(book1.getName())) {
                System.out.println("已经存在相同的书籍！");
                return;
            }
        }
        if (currentSize == bookList.getBooks().length) {
            System.out.println("书架满了，不再存放图书！");
        } else {
            bookList.setBooks(currentSize, book);
            //记得改变图书数量
            bookList.setUsedSize(currentSize + 1);
        }
    }
}
